/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package apus.action.person;

import apus.action.Action;
import apus.action.ActionResult;
import apus.entity.Person;
import apus.persistence.PersonDAO;
import apus.services.ServiceLocator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author 21vek
 */
public class PersonEditAction implements Action{

    @Override
    public String getName() {
        return "person.edit";
    }

    @Override
    public ActionResult execute(HttpServletRequest request, HttpServletResponse response) {
        if (request.getParameter("id") != null) {
            int id = Integer.parseInt(request.getParameter("id"));
            Person person = ServiceLocator.getService(PersonDAO.class).read(id);
            request.setAttribute("person", person);
            return ActionResult.forward("/WEB-INF/View/PersonView/EditPersonView.jsp");
        }
        List<Person> personList = ServiceLocator.getService(PersonDAO.class).findAll();
        request.setAttribute("personList", personList);
        return ActionResult.redirect("person.list");
    }
    
}
